{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4cb00389-91f0-4eca-8016-bff829f967e9",
   "metadata": {},
   "source": [
    "# Python Polite Guard Examples"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e24c20ff-c922-4746-b855-3d563ee58c52",
   "metadata": {},
   "source": [
    "## Basic Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9650ff8b-91cf-40d7-98cf-7e9cdc13b402",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "classifier = pipeline(\"text-classification\", \"Intel/polite-guard\")\n",
    "text = \"You look good today\"\n",
    "output = classifier(text)\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8f3dccc-08ce-4526-bb89-75df6beccf71",
   "metadata": {},
   "source": [
    "## Ipywidgets Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b705d8b-8a8c-4d36-a2df-31b7b6e6ad16",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipywidgets as widgets\n",
    "from IPython.display import display, clear_output\n",
    "from transformers import pipeline\n",
    "\n",
    "# Create a Text Box\n",
    "text_box = widgets.Text(\n",
    "    placeholder=\"Enter text here\",\n",
    "    description=\"Input:\"\n",
    ")\n",
    "\n",
    "# Create a Button\n",
    "button = widgets.Button(\n",
    "    description=\"Analyze\",\n",
    "    button_style=\"primary\"\n",
    ")\n",
    "\n",
    "# Create an Output Widget\n",
    "output = widgets.Output()\n",
    "\n",
    "# Function to Run When Button is Clicked\n",
    "def on_button_click(b):\n",
    "    with output:\n",
    "        clear_output(wait=True)  # Clear previous output\n",
    "        classifier = pipeline(\"text-classification\", \"Intel/polite-guard\")\n",
    "        result = classifier(text_box.value)\n",
    "        print(result)\n",
    "        #print(f\"{output}\")\n",
    "        print(f\"\\n'{text_box.value}' is {result[0]['label']}\")\n",
    "        #print(\"Hi\")\n",
    "\n",
    "# Link Button Click to Function\n",
    "button.on_click(on_button_click)\n",
    "\n",
    "# Display Widgets\n",
    "display(text_box, button, output)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1df3edd8-b11b-43fc-955f-848df221241c",
   "metadata": {},
   "source": [
    "## ONNX Runtime Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a296cf5d-aa08-4e01-80e3-37224a71ea76",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import AutoTokenizer, pipeline\n",
    "from optimum.onnxruntime import ORTModelForSequenceClassification\n",
    "from onnxruntime import SessionOptions\n",
    "\n",
    "# Create ONNX Runtime session options to avoid thread affinity issues\n",
    "sess_options = SessionOptions()\n",
    "sess_options.intra_op_num_threads = 1\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained(\"intel/polite-guard\")\n",
    "\n",
    "model = ORTModelForSequenceClassification.from_pretrained(\n",
    "    \"intel/polite-guard\",\n",
    "    file_name=\"onnx/model_quantized.onnx\",\n",
    "    session_options=sess_options\n",
    ")\n",
    "\n",
    "classifier = pipeline(\"text-classification\", model=model, tokenizer=tokenizer)\n",
    "\n",
    "output = classifier(\"You look good today\")\n",
    "print(output)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ai-project",
   "language": "python",
   "name": "ai-project"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
